home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-06 | 6.8 KB | 275 lines | [TEXT/KAHL] |
- // ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // • Program: Main
- // • File: Main.c
- // •
- // • Copyright © 1993 by Scott B. Steinman, O.D., Ph.D. All Rights Reserved.
- // ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
-
- #include "FrameAnim.h"
-
- // • ------------------ External Globals ----------------------------------
-
- extern PicHandle gFramePICTs[]; // • From Files.c file
-
- // • ------------------ Internal Globals ----------------------------------
-
- CWindowPtr gMainWindow; // • Window for animation display
- GWorldPtr gFrames[ kMaxFrames ]; // • Offscreen GWorlds for frames
- GDHandle gDevice; // • GDevice of display monitor
- PaletteHandle gPalette; // • Gray-scale palette
- CTabHandle gCTable; // • Gray-scale color table
- EventRecord gEvent; // • Event record for user event handling
- Handle gMenuBar; // • Handle to menu bar
- Settings gSettings; // • Animation settings
- Flags gFlags; // • General program flags
-
- // • ------------------ Static Functions ----------------------------------
-
- static void CheckEnvironment( void );
- static void InitParams( void );
- static void SetMonitorDepth( void );
- static void StartThings( void );
-
- // • ------------------ Check Hardware & Software Environment -------------
-
- static void
- CheckEnvironment( void )
- // • Confirm proper environment for running program.
- // • The program requires the Slot Manager for the VBlank timing and
- // • 32-bit QuickDraw, so it will not run on a Mac Plus, SE, SE/30,
- // • Portable or Classic I or II.
- {
- long gestResponse;
- short version;
- OSErr gestErr;
-
- gestErr = Gestalt( gestaltVersion, &gestResponse );
- if (!gestErr) {
-
- // • Ensure that machine is running System 7 or higher. If not, exit.
-
- Gestalt( gestaltSystemVersion, &gestResponse );
- version = HiWord( gestResponse );
- if (gestResponse < 7) { // • No NuBus slots
- StopAlert( kSystem7AlertID, NullPointer );
- ExitToShell();
- }
-
- // • Ensure that machine has NuBus slots
- // • for Slot Manager routines. If not, exit.
-
- Gestalt( gestaltNuBusConnectors, &gestResponse );
- if (gestResponse == 0) { // • No NuBus slots
- StopAlert( kMachineAlertID, NullPointer );
- ExitToShell();
- }
-
- // • Check QuickDraw version to ensure that machine has Palette Manager.
-
- Gestalt( gestaltQuickdrawVersion, &gestResponse );
- if (gestResponse == gestaltOriginalQD || gestResponse == gestalt8BitQD) {
- StopAlert( kQD32BitAlertID, NullPointer );
- ExitToShell();
- }
-
- // • Ensure that machine has FPU.
-
- Gestalt( gestaltFPUType, &gestResponse );
- if (gestResponse == gestaltNoFPU) {
- StopAlert( kFPUAlertID, NullPointer );
- ExitToShell();
- }
-
- // • Get number of monitors in system.
-
- GetNumMonitors();
- }
- else
- ExitToShell();
- }
-
- // •------------------- Set Monitor Color Depth ---------------------------
-
- static void
- SetMonitorDepth( void )
- // •
- // • Changes monitor depth to match desired depth of animation frames.
- {
- PixMapHandle pmh = NullHandle;
-
- gDevice = GetMainDevice();
- if (gSettings.numMonitors == 2 && gSettings.displayMonitor == kSecondary)
- gDevice = GetNextDevice( gDevice );
-
- HLock( (Handle) gDevice );
- pmh = (**gDevice).gdPMap;
- HLock( (Handle) pmh );
- gSettings.currDepth = (**pmh).pixelSize;
- HUnlock( (Handle) pmh );
- HUnlock( (Handle) gDevice );
-
- if (HasDepth( gDevice, kDepth, 1, 1 ) != 0)
- SetDepth( gDevice, kDepth, 1, 1 );
- else
- ErrorHandler( kNoDepthMsg, (char *) NilString, (char *) NilString,
- (char *) NilString );
- }
-
- // •------------------- Animation Parameter Set-up Routines ---------------
-
- static void
- InitParams( void )
- // •
- // • Used to initialize animation parameters.
- {
- short i;
-
- // • Ensure that pointers and handles are properly initialized to NULL
-
- gMenuBar = NullHandle;
- gPalette = NullHandle;
- gCTable = NullHandle;
- gMainWindow = NullPointer;
-
- for (i = 0; i < kMaxFrames; i++)
- gFrames[ i ] = NullPointer;
- for (i = 0; i < kMaxFrames; i++)
- gFramePICTs[ i ] = NullHandle;
-
- // • Initialize program flags
-
- gFlags.done = false;
- gFlags.cancel = false;
-
- // • Set default values for animation parameters
-
- gSettings.numFrames = 20; // • Total # of frames filmed
- gSettings.frameDelay = 3; // • Wait 3 VBlanks between frames.
- gSettings.frameSize = 190; // • Frame width (and height) in pixels
- gSettings.sizeDiff = 3; // • Target size change of 3 pixels
- gSettings.bkgndGray = 100;
- gSettings.targetGray = 10;
- }
-
- // • ------------------ Get Program Ready to Go ---------------------------
-
- static void
- StartThings( void )
- {
- SetMonitorDepth();
- SetUpWindows();
- SetUpMenus();
- InitCursor();
- FlushEvents( everyEvent, 0 );
- }
-
- // • ------------------ Free As Much Heap Space As Possible ---------------
-
- void
- MaximizeHeap( const long bytesNeeded )
- // •
- // • Get more contiguous heap space.
- {
- long totalBytes, contigBytes;
-
- // • If enough contiguous heap space, exit.
-
- if (MaxBlock() >= bytesNeeded)
- return;
-
- // • See how much space is possible.
-
- PurgeSpace( &totalBytes, &contigBytes );
-
- // • Free all purgeable, unlocked blocks and compact memory.
-
- PurgeMem( totalBytes );
- CompactMem( totalBytes );
- }
-
- // • ------------------ Error Message Printing Routine --------------------
-
- void
- ErrorHandler( const short stringNum, const char *optStr1, const char *optStr2,
- const char *optStr3 )
- // •
- // • Displays alert box with error message.
- {
- Str255 errorStr;
-
- CleanUp();
- GetIndString( errorStr, kErrorMessageStrsID, stringNum );
- ParamText( errorStr, optStr1, optStr2, optStr3 );
- StopAlert( kErrorAlertID, NullPointer );
- ExitToShell();
- }
-
- // •------------------- Convert Error Number To PString -------------------
-
- void
- ErrNumToPstr( const short iNum, char *str )
- // •
- // • Convert integer error code number to Pascal-format text string.
- {
- PtoCstr( str );
- sprintf( str, "%d", iNum );
- CtoPstr( str );
- }
-
- // • ------------------ Clean Up All Allocated Memory ---------------------
-
- void
- CleanUp( void )
- // •
- // • Deallocate film memory.
- {
- short i;
-
- // • Free any remaining animation frame GWorlds
- // • and frame Picture record
-
- DisposeFrames();
- DisposeFramePICTs();
-
- // • Free any remaining color records
-
- if (gPalette != NullHandle) {
- DisposePalette( gPalette );
- gPalette = NullHandle;
- }
- if (gCTable != NullHandle) {
- DisposCTable( gCTable );
- gCTable = NullHandle;
- }
-
- // • Free any remaining windows
-
- if (gMainWindow != NullPointer) {
- CloseWindow( gMainWindow );
- gMainWindow = NullPointer;
- }
-
- // • Reset monitor depth.
-
- SetDepth( gDevice, gSettings.currDepth, 1, 1 );
- }
-
- // • ------------------ Main Calling Program ------------------------------
-
- int
- main()
- // •
- // • Main calling function -
- // • It does initialization, then enters the event-handler loop.
- {
- InitManagers();
- CheckEnvironment();
- InitParams();
- if (gSettings.numMonitors == 2)
- MonitorDlg();
- StartThings();
-
- MainEventLoop();
- CleanUp();
- }
-